/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import okio from 'okio';

var byteStringObj = new okio.ByteString.ByteString('Hello world');

export default {
    data: {
        decodeBase64Value: "",
        decodeHexValue: "",
        encodeUtf8Value: "",
        ofOutputValue: "",
        asciiLowercaseValue: "",
        asciiUppercaseValue: "",
        byteArrayValue: "",
        internalArrayValue: "",
        hashCodeValue:"",
        compareToValue : "Hello world",
        compareToResult :"",
        inputGetIndex: '',
        getIndexValue: "",
        encodeBase64Value:"",
        encodeHexValue:"",
        encodemd5HexValue:""
    },
    onInit() {
        this.byteString();
    },
    encodeBase64() {
        let encodeBase64 = byteStringObj.encodeUtf8('Hello world').Base64();
        this.encodeBase64Value = JSON.stringify(encodeBase64);
    },
    decodeBase64() {
        let decodeBase64 = byteStringObj.decodeBase64('SGVsbG8gd29ybGQ=');
        this.decodeBase64Value = JSON.stringify(decodeBase64);
    },
    encodeHex() {
        let encodeHex = byteStringObj.encodeUtf8('Hello world').Hex();
        this.encodeHexValue = JSON.stringify(encodeHex);
    },
    encodeMD5Hex() {
        let encodeHex = byteStringObj.encodeUtf8('Hello world test test').md5().Hex();
        this.encodemd5HexValue = JSON.stringify(encodeHex);
    },
    decodeHex() {
        let decodehex = byteStringObj.decodeHex('48656C6C6F20776F726C640D0A');
        this.decodeHexValue = JSON.stringify(decodehex);
    },
    encodeUtf8() {
        let encodeUtf8 = byteStringObj.encodeUtf8('Hello world #4 ❤ ( ͡ㆆ ͜ʖ ͡ㆆ)');
        this.encodeUtf8Value = JSON.stringify(encodeUtf8);
    },
    ofValue() {
        let ofData = byteStringObj.of(["Hello", "world"]);
        this.ofOutputValue = JSON.stringify(ofData);
    },
    toAsciiLowercaseValue() {
        let toAsciiLowercase = byteStringObj.toAsciiLowercase('HELLO WORLD');
        this.asciiLowercaseValue = JSON.stringify(toAsciiLowercase);
    },
    toAsciiUppercaseValue() {
        let toAsciiUppercase = byteStringObj.toAsciiUppercase('hello world');
        this.asciiUppercaseValue = JSON.stringify(toAsciiUppercase);
    },
    toByteArrayValue() {
        let toByteArray = byteStringObj.toByteArray();
        this.byteArrayValue = JSON.stringify(toByteArray);
    },
    internalArray() {
        let dataString = byteStringObj.internalArray();
        this.internalArrayValue = JSON.stringify(dataString);
    },
    hashCode() {
        let hashcode = byteStringObj.hashCode();
        this.hashCodeValue = JSON.stringify(hashcode);
    },
    compareInputChange(e) {
        this.compareToValue = e.text;
    },
    compareToByteData() {
        var byteA = new okio.ByteString.ByteString('10');
        var byteB = new okio.ByteString.ByteString(this.compareToValue);
        this.compareToResult = byteA.compareToOther(byteB);
    },
    getIndexInputChange(e) {
        this.inputGetIndex = e.text;
    },
    getIndex() {
        // pass index(buffer's string value) for which you want to retrieve value
        this.getIndexValue = byteStringObj.getWithIndex(this.inputGetIndex);
    },
    inputDataChange(e){
        this.inputData = e.text;
    },
    getInputLength(){
        var byteStrObj = new okio.ByteString.ByteString(this.inputData);
        this.inputDataLength = byteStrObj.getSize();
    },
    byteString() {
        ///  API Methods.
        // 1. decodeBase64 - returns byte array.
        let decodeBase64 = byteStringObj.decodeBase64('SGVsbG8gd29ybGQ=');

        // 2. decodeHex
        let decodehex = byteStringObj.decodeHex('48656C6C6F20776F726C640D0A');

        //3. encodeUtf8
        let encodeUtf8 = byteStringObj.encodeUtf8('Hello world #4 ❤ ( ͡ㆆ ͜ʖ ͡ㆆ)');

        //4.  of ()
        let ofData = byteStringObj.of(["Hello", "world"]);

        //5.  toAsciiLowercaseValue()
        let toAsciiLowercase = byteStringObj.toAsciiLowercase('HELLO WORLD');

        //6.  toAsciiUppercaseValue()
        let toAsciiUppercase = byteStringObj.toAsciiUppercase('hello world');

         //7.  toByteArrayValue()
        let toByteArray = byteStringObj.toByteArray();

        // 8. internalArray
        let dataString = byteStringObj.internalArray();
    
        //9. HashCode
        let hashcode = byteStringObj.hashCode();
        
        // 10. compareTo
        var byteA = new okio.ByteString.ByteString('10');
        var byteB = new okio.ByteString.ByteString('10');
        var result = byteA.compareToOther(byteB);
    }
}
